
package org.lq.ht.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.ht.dao.VipDao;
import org.lq.ht.entity.Vip;
import org.lq.ht.util.Instantiation;
import org.lq.ht.util.JDBCUtil;
/**
 * 会员接口实现
 * @author 郑奥宇
 * @phone 15993025053
 *
 * @package org.lq.ht.dao
 * @date 2020年8月20日下午10:21:55
 */
public class VipDaoImpl implements VipDao,Instantiation<Vip>{
	
	/**
	 * 添加
	 */
	@Override
	public int save(Vip t) {
		int num = 0;
		String sql = "insert into vip(vid,password,discount,name,phone,id_card,address,integral,type,handlecarddate,note)"
				+"values(?,?,?,?,?,?,?,?,?,now(),?)";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getVid(),t.getPassword(),t.getDiscount(),t.getName(),t.getPhone(),t.getId_card(),
					t.getAddress(),t.getIntegral(),t.getType(),t.getNote());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 修改
	 */
	@Override
	public int update(Vip t) {
		int num= 0;
		String sql = "update vip set vid=?,password=?,discount=?,name=?,phone=?,id_card=?,address=?,integral=?,type=?,handlecarddate=now(),note=? where no=?";
		try {
			num = JDBCUtil.executeUpdate(sql, t.getVid(),t.getPassword(),t.getDiscount(),t.getName(),t.getPhone(),t.getId_card(),
					t.getAddress(),t.getIntegral(),t.getType(),t.getNote(),t.getNo());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 删除
	 */
	@Override
	public int delete(int no) {
		int num = 0;
		String sql = "delete from vip where no = ?";
		try {
			num = JDBCUtil.executeUpdate(sql, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}
	/**
	 * 查询全部
	 */
	@Override
	public List<Vip> findAll() {
		String sql = "select * from vip";
		List<Vip> list = new ArrayList<>();
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 通过序号查询
	 */
	@Override
	public Vip getById(int no) {
		List<Vip> list = new ArrayList<>();
		String sql = "select * from vip where  no = ?";
		try {
			list = JDBCUtil.executeQuery(sql, this, no);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()>0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Vip instance(ResultSet rs) {
		Vip vip =new Vip();
		try {
			vip.setVid(rs.getInt("vid"));
			vip.setPassword(rs.getString("password"));
			vip.setDiscount(rs.getString("discount"));
			vip.setName(rs.getString("name"));
			vip.setPhone(rs.getString("phone"));
			vip.setId_card(rs.getString("id_card"));
			vip.setAddress(rs.getString("address"));
			vip.setIntegral(rs.getInt("integral"));
			vip.setType(rs.getInt("type"));
			vip.setHandlecarddate(rs.getTimestamp("handlecarddate"));
			vip.setNote(rs.getString("note"));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return vip;
		
	}

}

